package com.xuxin.baby.popup;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.lxj.xpopup.animator.PopupAnimator;
import com.lxj.xpopup.core.CenterPopupView;
import com.xuxin.baby.R;

/**
 * Created by xu_xin
 * Created on 2020/11/30 0030
 */
public class UpdatePopup extends CenterPopupView implements View.OnClickListener {
    private ProgressBar popup_update_progress;
    private Button popup_update_confirm;
    private Button popup_update_cancel;
    private TextView popup_update_percent;

    public UpdatePopup(@NonNull Context context) {
        super(context);
    }

    public UpdatePopup(@NonNull Context context, OnUpdateConfirmListener onUpdateConfirmListener) {
        super(context);
        this.onUpdateConfirmListener = onUpdateConfirmListener;
    }

    private OnUpdateConfirmListener onUpdateConfirmListener;

    public interface OnUpdateConfirmListener {
        void OnUpdateConfirm();
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_update;
    }

    public void setProgressMax(long progress) {
        popup_update_confirm.setVisibility(GONE);
        popup_update_cancel.setText("隐藏");
        if (null != popup_update_progress) {
            popup_update_progress.setMax((int) progress);
        }
    }

    public void updateProgress(String speed, long progress) {
        if (null != popup_update_progress) {
            popup_update_progress.setProgress((int) progress);
            String percent = (int) (progress * 100 / popup_update_progress.getMax()) + " %";
            popup_update_percent.setText(percent + " " + speed);
            LogUtils.e("percent = " + percent);
        }
    }

    @Override
    protected void onCreate() {
        popup_update_progress = findViewById(R.id.popup_update_progress);
        popup_update_confirm = findViewById(R.id.popup_update_confirm);
        popup_update_cancel = findViewById(R.id.popup_update_cancel);
        popup_update_percent = findViewById(R.id.popup_update_percent);
        popup_update_confirm.setOnClickListener(this);
        popup_update_cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.popup_update_confirm) {
            if (null != onUpdateConfirmListener) {
                onUpdateConfirmListener.OnUpdateConfirm();
            }
        } else if (id == R.id.popup_update_cancel) {
            dismiss();
        }
    }

    @Override
    protected int getMaxWidth() {
        return (int) (ScreenUtils.getScreenWidth() * 0.75);
    }

    @Override
    protected int getMaxHeight() {
        return (int) (ScreenUtils.getScreenWidth() * 0.75);
    }

    @Override
    protected PopupAnimator getPopupAnimator() {
        return super.getPopupAnimator();
    }

    protected int getPopupWidth() {
        return 0;
    }

    protected int getPopupHeight() {
        return 0;
    }
}